package com.cqie.rbac.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqie.rbac.entity.Quota;
import com.cqie.rbac.entity.QuotaOptions;
import com.cqie.rbac.mapper.QuotaMapper;
import com.cqie.rbac.mapper.QuotaOptionsMapper;
import com.cqie.rbac.service.IQuotaService;
import com.cqie.rbac.util.*;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 夏兵
 * @Date 2021/10/12
 */
@Service
@Log4j
public class QuotaServiceImpl extends ServiceImpl<QuotaMapper, Quota> implements IQuotaService {

    @Autowired
    private TotalListener totalListener;

    @Autowired
    private DynamicListener dynamicListener;

    @Autowired
    private QuotaMapper quotaMapper;

    @Autowired
    private QuotaOptionsMapper quotaOptionsMapper;

    @Override
    @Async("threadPool")
    public void excelImportQuota(InputStream inputStream) {
        try {
            ExcelReader excelReader = EasyExcel.read(inputStream, null, dynamicListener).headRowNumber(2).build();
            for (ReadSheet readSheet : excelReader.excelExecutor().sheetList()) {
                excelReader.read(readSheet);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("excel导入指标报错");
        }
    }

    @Override
    public Result treeOfQuota() {
        LambdaQueryWrapper<Quota> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Quota::getParentId, 0)
                .orderByAsc(Quota::getSortCode);
        List<Quota> quotas = quotaMapper.selectList(queryWrapper);
        ArrayList<QuotaTree> quotaTrees = new ArrayList<>();
        for (Quota quota : quotas) {
            QuotaTree quotaTree = new QuotaTree();
            quotaTree.setQuotaId(quota.getId());
            quotaTree.setName(quota.getQuotaName());
            quotaTree.setWeight(quota.getWeight());
            quotaTree.setUpdateTime(quota.getUpdateTime());
            quotaTree.setChildren(setQuotaChild(quota.getId()));
            quotaTrees.add(quotaTree);
        }
        return Result.ok().data(quotaTrees);
    }

    @Override
    public Result queryOptionsByQuotaId(Integer quotaId) {
        LambdaQueryWrapper<QuotaOptions> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(quotaId != null && quotaId > 0, QuotaOptions::getQuotaId, quotaId);
        return Result.ok().data(quotaOptionsMapper.selectList(lambdaQueryWrapper));
    }

    public List<QuotaTree> setQuotaChild(Integer quotaFatherId) {
        LambdaQueryWrapper<Quota> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Quota::getParentId, quotaFatherId);
        List<Quota> quotaChildren = quotaMapper.selectList(queryWrapper);
        if (quotaChildren == null || quotaChildren.size() < 1) {
            return null;
        }
        ArrayList<QuotaTree> quotaTrees = new ArrayList<>(quotaChildren.size());
        for (Quota quotaChild : quotaChildren) {
            QuotaTree quotaTree = new QuotaTree();
            quotaTree.setQuotaId(quotaChild.getId());
            quotaTree.setName(quotaChild.getQuotaName());
            quotaTree.setWeight(quotaChild.getWeight());
            quotaTree.setUpdateTime(quotaChild.getUpdateTime());
            quotaTree.setChildren(setQuotaChild(quotaChild.getId()));
            LambdaQueryWrapper<QuotaOptions> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(QuotaOptions::getQuotaId, quotaChild.getId());
            Integer count = quotaOptionsMapper.selectCount(lambdaQueryWrapper);
            if (count > 0) {
                quotaTree.setHasOptions(true);
            }
            quotaTrees.add(quotaTree);
        }
        return quotaTrees;
    }
}
